#include <rtthread.h>
#include <chassis.hpp>
#include <dr16.hpp>
#include <gimbal.hpp>
#include <minipc.hpp>
#include <shoot.hpp>
#include "referee.hpp"
#include "supercap.hpp"
#include "usart.h"
#include "power_control.hpp"
#include "flysky.hpp"

/**
 * @brief 串口回调函数
 *
 * @param huart
 */
void UART_IdleRxCallback(UART_HandleTypeDef *huart) {
    rt_interrupt_enter();

    if (huart == &DBUS_USART) {
				#if (USE_SBUS)
				if (huart->RxXferSize - ((DMA_Stream_TypeDef *)huart->hdmarx->Instance)->NDTR == 25)
           flysky.decode();
				#endif
				#if (!USE_SBUS)
        if (huart->RxXferSize - ((DMA_Stream_TypeDef *)huart->hdmarx->Instance)->NDTR == 18)
           dr16.decode();
					  #endif
    } 
		if (huart == &REFEREE_UART) {
        Referee_Decode(RefereeRecvBuf);
    }
		else if (huart == &VT_USART) {
				vt.VT_Decode(VTRecvBuf);
		}

    rt_interrupt_leave();
}
/**
 * @brief can中断回调
 *
 * @param hcan
 */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
    rt_interrupt_enter();

    CAN_RxHeaderTypeDef rx_header;
    uint8_t             rx_data[8];

    HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, rx_data);

    if (hcan->Instance == CAN1) 
		{



				if ((rx_header.StdId - 0x204) >= 1) {
            gimbal.update(rx_header.StdId - 0x204, rx_data);
        }
				if((rx_header.StdId - 0x200) <= 4)
				{		
					  chassis.update(rx_header.StdId - 0x200, rx_data);
				}
				if (rx_header.StdId == 0x400)
        {
          power.powerupdate (rx_data,1);
        } 
				if (rx_header.StdId == 0x3FF)
        {
          power.powerupdate(rx_data,0);
				}

		}
		else if (hcan->Instance == CAN2) 
		{

        if (rx_header.StdId - 0x200 <= 4) 
				{
					shoot.update(rx_header.StdId - 0x200, rx_data);
				}

        
		}

    rt_interrupt_leave();
}
